package com.wswy.wzcx.ui.persenter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputFilter.LengthFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import android.widget.TextView;
import com.blankj.utilcode.util.SizeUtils;
import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.api.ProgressDialogObserver;
import com.che.libcommon.utils.RxBus;
import com.wswy.wzcx.R;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.model.JZCFMode;
import com.wswy.wzcx.module.InternalMsgCode;
import com.wswy.wzcx.statistics.StatTools;
import com.wswy.wzcx.statistics.StatisticsId;
import com.wswy.wzcx.ui.activity.AddDriverLicenseActivity;
import com.wswy.wzcx.ui.other.AndroidUtilities;
import com.wswy.wzcx.ui.other.LayoutHelper;
import com.wswy.wzcx.ui.view.JZCFAddView;
import com.wswy.wzcx.widget.home.TipLayoutView;
import com.wswy.wzcx.widget.home.TipLayoutView.BindCallBack;
import com.wswy.wzcx.widget.home.TipLayoutView.ResultFilter;
import com.wswy.wzcx.widget.home.TipMap;

/**
 * Created by wangduheng26 on 2018/3/16.
 * wangduheng26@gmail.com
 */

public class JZCFAddPresenter {
    private Context mContext;
    TipLayoutView layoutNumber, layoutArchivesNumber, layoutDate, layoutNike;
    private JZCFAddView mView;

    public JZCFAddPresenter(Context context, JZCFAddView view) {
        this.mContext = context;
        mView = view;
    }

    private JZCFMode mode;

    public void onCreate(FrameLayout frameLayout, Intent intent) {
        if (intent != null && intent.hasExtra("data")) {
            mode = intent.getParcelableExtra("data");
        }
        Context context = mContext;
        ScrollView scrollView = new ScrollView(context);
        LinearLayout layout = new LinearLayout(context);
        layout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        layout.setDividerPadding(AndroidUtilities.dp(15));
        layout.setDividerDrawable(ContextCompat.getDrawable(context, R.drawable.list_divider_h));
        layout.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams params = LayoutHelper.createLinear(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT);

        TipMap tipMap = new TipMap.Builder()
                .topTip("驾驶证号码")
                .showHelp(R.drawable.jiashizheng_zhengye)
                .build();

        layoutNumber = new TipLayoutView(context, tipMap, new BindCallBack() {
            @Override
            public void onBindView(TextView t) {
                t.setHint("请输入15或18位驾驶证号码");
                t.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                if (t instanceof EditText) {
                    ((EditText) t).setFilters(new InputFilter[]{new LengthFilter(18)});
                }
                if (mode != null) {
                    t.setText(mode.getNumber());
                }
            }
        }, new ResultFilter() {
            @Override
            public boolean filter(@Nullable CharSequence editable) {
                return editable != null && (editable.length()==15||editable.length() == 18);
            }
        });

//        layoutNumber = new TipLayoutView(context, tipMap,
//                t -> {
//                    t.setHint("请输入15或18位驾驶证号码");
//                    t.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
//                    if (t instanceof EditText){
//                        ((EditText) t).setFilters(new InputFilter[]{new LengthFilter(18)});
//                    }
//                    if (mode != null)
//                        t.setText(mode.getNumber());
////                    t.setInputType(InputType.TYPE_CLASS_NUMBER);
//                }, editable -> editable != null && (editable.length()==15||editable.length() == 18));
        layout.addView(layoutNumber, params);

        tipMap = new TipMap.Builder()
                .topTip("驾驶证档案编号")
        .showHelp(R.drawable.jiashizheng_fuben)
                .build();
        layoutArchivesNumber = new TipLayoutView(context, tipMap, new BindCallBack() {
            @Override
            public void onBindView(TextView t) {
                t.setHint("请输入12位档案编号");
                t.setInputType(InputType.TYPE_CLASS_NUMBER);
                if (t instanceof EditText) {
                    ((EditText) t).setFilters(new InputFilter[]{new LengthFilter(18)});
                }
                if (mode != null) {
                    t.setText(mode.getFileNumber());
                }
            }
        }, new ResultFilter() {
            @Override
            public boolean filter(@Nullable CharSequence editable) {
                return editable!=null&&editable.length() == 12;
            }
        });
        layout.addView(layoutArchivesNumber, params);

        tipMap = new TipMap.Builder()
                .topTip("初次领证日期")
                .type(TipMap.ContentType.TYPE_DATE)
                .build();
        layoutDate = new TipLayoutView(context, tipMap, new BindCallBack() {
            @Override
            public void onBindView(TextView text) {
                if (mode != null)
                    text.setText(mode.getInitDate());
            }
        });

        if (mode != null && !TextUtils.isEmpty(mode.getInitDate())){
            layoutDate.setContext(mode.getInitDate());
        }else {
            layoutDate.setContext(DateFormat.format("yyyy-MM-dd",System.currentTimeMillis()).toString());
        }
        layout.addView(layoutDate, params);

        tipMap = new TipMap.Builder()
                .topTip("持证人姓名")
                .build();
        layoutNike = new TipLayoutView(context, tipMap, new BindCallBack() {
            @Override
            public void onBindView(TextView text) {
                text.setHint("请输入持证人姓名");
                if (text instanceof EditText){
                    ((EditText) text).setFilters(new InputFilter[]{new LengthFilter(16)});
                }
                if (mode != null)
                    text.setText(mode.getNikeName());
            }
        });
        layout.addView(layoutNike, params);

        scrollView.addView(layout, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT));
        LayoutParams frame = LayoutHelper
            .createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT);
        frame.topMargin = SizeUtils.dp2px(10);
        frameLayout.addView(scrollView,
            frame);
    }

    public void onDestroy() {
        mContext = null;
    }

    public void query() {
        String numberContent = layoutNumber.getContent();
        if (TextUtils.isEmpty(numberContent)) return;
        String archivesContent = layoutArchivesNumber.getContent();
        if (TextUtils.isEmpty(archivesContent)) return;
        String dateContent = layoutDate.getContent();
        if (TextUtils.isEmpty(dateContent)) return;
        String nikeContent = layoutNike.getContent();
        if (TextUtils.isEmpty(nikeContent)) return;

        JZCFMode jzcfMode = new JZCFMode("", archivesContent, "", dateContent, numberContent, nikeContent, 0);
        if (mode != null) {
            jzcfMode.set_id(mode.get_id());
            jzcfMode.setCreateUserId(mode.getCreateUserId());
        }else {

            StatTools.sendClick(mView.activity(), StatisticsId.clickJZCFAddQuery);

        }
        Api.get().submitJZCFMode(jzcfMode)
                .subscribe(new ProgressDialogObserver<JZCFMode>(mContext, "提示", "正在处理") {
                    @Override
                    protected void onApiResult(@Nullable JZCFMode s) {
                        Intent intent = new Intent(AddDriverLicenseActivity.ACTION_QUERY_JZCF);
                        intent.putExtra("data", s);
                        intent.putExtra("isEdit", mode != null);
                        LocalBroadcastManager.getInstance(mContext).sendBroadcast(intent);

                        RxBus.getDefault().postWithCode(InternalMsgCode.CODE_ADD_JZ,s);

                        mView.activity().finish();
                    }

                    @Override
                    protected void onApiError(@Nullable BaseResult result) {
                        toastErrorMsg(result);
                    }
                });


    }
}
